import http from "@/config/http/http.js";
import fingerprint from "@fingerprintjs/fingerprintjs";
import axios from "axios";

let loginFn = null;
export default {
  // login
  async userLogin(context) {
    // 如果已经发送了一个登录请求,这不再重新发送
    if (loginFn) return await loginFn;
    if (context.state.userToken) {
      return new Promise.resolve();
    }
    let postData = {};
    // 获取浏览器指纹
    let info = await fingerprint.load().then((res) => res.get());
    postData.accountNumber = info.visitorId;
    const getInfo = async () => {
      const userInfo = await http("login", postData).then((res) => {
        context.commit("setToken", res.data.token);
        context.commit("setUserInfo", res.data.userInfo);
        return res;
      });
      return userInfo;
    };

    let result = getInfo();
    loginFn = result;
    await result;
  },

  // 获取系统信息
  async getSystemInfo(context) {
    let url =
      "https://admin.dongping.link/jsonApi/efd54c9666c639cba531a1a5ea912066";

    let { data: info } = await axios.get(url);

    context.commit("setSystemInfo", info.data);
    return info.data;
  },
  //   获取博主信息
  async getBloggerInfo(context) {
    console.log("获取博主信息,'获取博主信息");
    // 获取博主信息
    return http("getBloggerInfo").then((res) => {
      context.commit("setBlogger", res.data);
      return res;
    });
  },
};
